<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Options extends AdminController {
	
	function Options(){
		parent::AdminController();		
		$this->load->config("settings");
		$this->_setPageView("options/index");
	}
	
	function index($group = "default"){
		$config_group = $this->config->item("groups");
		
		if(!isset($config_group[$group])){
			$this->_addError("No existe grupo de configuración seleccionado. Tratando las predefinidas");
			$group = "default";
		}
		
		if($this->input->post('submit')){
			//unset($_POST['submit']);
			$this->settings->set_settings($_POST, TRUE);
			
			$this->_addFlashSuccess("Opciones editadas correctamente");
			
			//redirigir bien
			redirect("admin/options/$group");
		}
		
		$process_params = $this->process_config($config_group[$group]['params']);

		$this->_addData("params", $process_params);
		$this->_render();
	}
	
	
	function process_config($config){
		$process_params = array();
		foreach($config as $param_key=>$param_info){
			
			$param_value = $this->settings->item($param_key);
			
			if($param_value != NULL){
				$param_info['value'] = $param_value;
			}
			
			$process_params[$param_key] = $param_info;
		}
		
		return $process_params;
	}
	
	function _remap($group = "default"){
		$this->index($group);
	}
	
}